package com.android.boncen.commonFrame.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.boncen.commonFrame.R;

/**
 * Created by boncen on 17-1-26.
 */

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {

    private final Context context;
    private String[] itemsName;
    private int[] imgs;


    public MyRecyclerViewAdapter(Context context, String[] itemsName, int[] imgs) {
        this.context = context;
        this.itemsName = itemsName;
        this.imgs = imgs;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView=View.inflate(context, R.layout.recycler_item_layout,null);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        String data = itemsName[position];
        holder.item_text.setText(data);
        holder.item_pic.setImageResource(imgs[position]);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener!=null){
                    onItemClickListener.OnItemClick(v,position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return itemsName.length;
    }



    class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView item_pic;
        private TextView item_text;
        private View itemView;
        public ViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            item_pic = (ImageView) itemView.findViewById(R.id.recyclerview_item_pic);
            item_text = (TextView) itemView.findViewById(R.id.recyclerview_item_text);

        }
    }

    public interface OnItemClickListener{
        /**
         *当recycleview某个item被点击时回调
         * @param view 点击item的视图
         */
        public void  OnItemClick(View view,int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
